//
//  VisitorView.swift
//  xlwb-swift
//
//  Created by 王世清 on 16/4/17.
//  Copyright © 2016年 wsq. All rights reserved.
//

import UIKit
/// 1 定义代理
protocol VisitorViewDelegate: NSObjectProtocol {
    //不会被复用，不许要把自己传出去
    func registerAction()
    func loginAction()
    
}
///

class VisitorView: UIView {
    /// 2 定义属性
    weak var delegate: VisitorViewDelegate?
    /// 3监听方法
    @objc func clickRegister() {
        delegate?.registerAction()
    }
    @objc func clickLogin() {
        delegate?.loginAction()
    }
    
    
    // MARK: -加载访客视图方法
    func setupInfo(imageName:String?, title:String) {
        messageLabel.text = title
        guard let imgName = imageName else {
            startAnimation()
            
            return
        }
        iconView.image = UIImage(named: imgName)
        //隐藏
        homeIconView.hidden = true
        //将遮罩移到底层
        sendSubviewToBack(maskedView)
    }
    
    private func startAnimation() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        //----------------------
        anim.removedOnCompletion = false
        
        iconView.layer.addAnimation(anim, forKey: nil)
        
    }
    
    // MARK: -构造函数
    // initWithFrame 是 UIview的指定构造函数
    // 纯代码开发
    override init(frame: CGRect) {
        super.init(frame: frame)
        //调用添加图标方法------------1
        setupUI()
    }

    // initWithCoder 是 使用 SB & xib 加载的函数
    required init?(coder aDecoder: NSCoder) {
        // 阻止加载 SB & xib ,使用纯代码自定义view
        //fatalError("init(coder:) has not been implemented")
        // 允许使用 SB & xib ,同时可以使用纯代码的view
        super.init(coder: aDecoder)
        //调用添加图标方法-------------2
        setupUI()
    }
    
    // MARK: -懒加载图标
    ///圈圈
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    ///遮罩
    private lazy var maskedView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    ///房子
    private lazy var homeIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    ///文字
    private lazy var messageLabel: UILabel = UILabel(title: "")
    ///注册按钮
    private lazy var registerButton: UIButton = UIButton(title: "注册", color: UIColor.orangeColor(), backImage: "common_button_white_disable")

    ///登录按钮
    private lazy var LoginButton: UIButton = UIButton(title: "登录", color: UIColor.orangeColor(), backImage: "common_button_white_disable")
    

}

extension VisitorView {
    ///添加图标的方法
    private func setupUI() {
        addSubview(iconView)
        addSubview(maskedView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(LoginButton)
        //设置可以使用自动布局 translatesAutoresizingMaskIntoConstraints
        //true 使用setFrame 设置布局
        //false 使用自动布局
        //iconView.translatesAutoresizingMaskIntoConstraints = false
        //简写：
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        //设置自动布局
        //1>圈圈
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: 0))
        //2>房子
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: 0))
        //3>文字
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Top, relatedBy: .Equal, toItem: iconView, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 224))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
        //4>注册按钮
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Left, relatedBy: .Equal, toItem: messageLabel, attribute: .Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
        //5>登录按钮
        addConstraint(NSLayoutConstraint(item: LoginButton, attribute: .Right, relatedBy: .Equal, toItem: messageLabel, attribute: .Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: LoginButton, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: LoginButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: LoginButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
        //6>遮罩
        //VFL
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[mask]-0-|", options: [], metrics: nil, views: ["mask": maskedView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[mask]-(btnHeight)-[register]", options: [], metrics: ["btnHeight": -36], views: ["mask": maskedView, "register": registerButton]))
        //设置背景颜色
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
        
        //按钮注册点击事件
        registerButton.addTarget(self, action: "clickRegister", forControlEvents: UIControlEvents.TouchUpInside)
        LoginButton.addTarget(self, action: "clickLogin", forControlEvents: UIControlEvents.TouchUpInside)
        
    }
    
    
    
    
}











